package org.midy.infrastructure.tools;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class IdCardTool {
    /**
     * 生成一个身份证号
     * @return
     */
    public String getIDCard(){
        String idNo = get17ID();
        idNo = idNo + getVerify(idNo);
        return idNo;
    }

    /**
     * 根据17位证件号生成最后一位校验码
     * @return
     */
    public final static int[] wi = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; // 每位的加权因子
    public final static int[] vi = {1,0,'X',9,8,7,6,5,4,3,2}; // 第18位的校验码
    public String getVerify(String idNo) {
        int remaining = 0;
        if(idNo != null && idNo.length() == 17) {
            int sum = 0;
            for(int i = 0; i < 17; i++) {
                String c = idNo.substring(i, i+1);
                int curr = Integer.valueOf(c);
                sum = sum + wi[i] * curr;
            }
            remaining = sum % 11;
            return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
        } else {
            throw new RuntimeException("idNo 出错啦");
        }
    }


    /**
     * 随机生成17位身份编码
     * @return 省 + 市 + 县 + 出生年月日 + 当日出生序列号 + 性别
     */
    public String get17ID() {
        StringBuilder idCard = new StringBuilder(17);
        idCard.append(this.getSheng()).append(this.getShi()).append(this.getXian())
                .append(this.getYear()).append(this.getMonth()).append(this.getDay())
                .append(this.getSequence()).append(this.getSex());
        return null;
    }

    private String getSex() {
        int sequence = ThreadLocalRandom.current().nextInt(10);
        return String.valueOf(sequence);
    }

    private String getSequence() {
        int sequence = ThreadLocalRandom.current().nextInt(99) + 1;
        if(sequence < 10) {
            return "0" + sequence;
        } else {
            return String.valueOf(sequence);
        }
    }

    private String  getDay() {
        int day = ThreadLocalRandom.current().nextInt(28) + 1;
        if(day < 10) {
            return "0" + day;
        } else {
            return String.valueOf(day);
        }

    }

    private String getMonth() {
        int month = ThreadLocalRandom.current().nextInt(12) + 1;
        if(month< 10) {
            return  "0" + month;
        } else {
            return String.valueOf(month);
        }
    }

    private String getYear() {
        int i = ThreadLocalRandom.current().nextInt(2010-1960) + 1960;
        if(i < 10) {
            return "0" + i;
        } else {
            return String.valueOf(i);
        }
    }

    private String getXian() {
        int i = ThreadLocalRandom.current().nextInt(4) + 1;
        if(i < 10) {
            return "0" + i;
        } else {
            return String.valueOf(i);
        }
    }

    private String getShi() {
        int i = ThreadLocalRandom.current().nextInt(2) + 1;
        if(i < 10) {
            return "0" + i;
        } else {
            return String.valueOf(i);
        }
    }

    /**
     * ‌北京市‌：110000
     * ‌天津市‌：120000
     * ‌河北省‌：130000
     * ‌山西省‌：140000
     * ‌内蒙古自治区‌：150000
     * ‌辽宁省‌：210000
     * ‌吉林省‌：220000
     * ‌黑龙江省‌：230000
     * ‌上海市‌：310000
     * ‌江苏省‌：320000
     * ‌浙江省‌：330000
     * ‌安徽省‌：340000
     * ‌福建省‌：350000
     * ‌江西省‌：360000
     * ‌山东省‌：370000
     * ‌河南省‌：410000
     * ‌湖北省‌：420000
     * ‌湖南省‌：430000
     * ‌广东省‌：440000
     * ‌广西壮族自治区‌：450000
     * ‌海南省‌：460000
     * ‌重庆市‌：500000
     * ‌四川省‌：510000
     * ‌贵州省‌：520000
     * ‌云南省‌：530000
     * ‌西藏自治区‌：540000
     * ‌陕西省‌：610000
     * ‌甘肃省‌：620000
     * ‌青海省‌：630000
     * ‌宁夏回族自治区‌：640000
     * ‌新疆维吾尔自治区‌：650000‌
     *
     */
    static final String[] shengFen = {"11","12","13","14","15","21","22","23","31","32","33","34","35","36","37","41","42","43","44","45","46","50","51","52","53","54","61","62","63","64","65"};
    private String getSheng() {
        return shengFen[ThreadLocalRandom.current().nextInt(shengFen.length)];
    }

    public static void main(String[] args) {
        IdCardTool idCardTool = new IdCardTool();
        ArrayList<String> errCache = new ArrayList<>();
        for(int i = 10000; i < 1000; i++) {
            String idCard = idCardTool.getIDCard();
            System.out.println(idCard);
            if(idCard.length() != 18) {
                errCache.add(idCard);
            }
        }

        if(errCache.isEmpty()) {
            System.out.println(" 没哟错误数据");
        } else {
            System.out.println("存在错误数据");
            for(String err: errCache ) {
                System.out.println("错误数据：" + err);
            }
        }
    }
}
